package Presentacion.academico;


import clases.academico.Matricula;
import global.Dialogos;
import global.Iconos;
import javax.swing.DefaultListModel;
import presentacion.academico.frmConsultaMatriculaEspeciales;

public class dlgAsignacionAlumnoCursoEspeciales extends javax.swing.JDialog {

    private DefaultListModel modelonormal;
    private DefaultListModel modeloespecial;
    private Matricula objMatricula;
    private Iconos ico;

    public dlgAsignacionAlumnoCursoEspeciales(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setIconos();
        objMatricula = new Matricula();
        setTitle("MATRICULACION DE ALUMNOS A CURSOS ESPECIALES");
        objMatricula.curso.listarTipoCursos(cboCurso, true);
        objMatricula.curso.listarTipoCursos(cboCursoEspecial, false);
        setLocationRelativeTo(this);
        setResizable(false);
        iniciar();
    }
      private void setIconos(){
          ico=new Iconos();
   btnnuevo.setIcon(ico.getNuevo());
    btnactualizar.setIcon(ico.getActualizar());
    btnguardar.setIcon(ico.getGuardar());
    btnsalir.setIcon(ico.getSalir());
    }
    
    private void iniciar() {
        cboCurso.setSelectedIndex(0);
        cboCursoEspecial.setSelectedIndex(0);
        modelonormal = new DefaultListModel();
        lCurso.setModel(modelonormal);
        modeloespecial = new DefaultListModel();
        lCursoEspecial.setModel(modeloespecial);
        visiblidad(true);
        hablitarBotones();

    }

    public void visiblidad(boolean visibilidad) {
        if (visibilidad != true) {
            btnactualizar.setVisible(true);
            btnguardar.setVisible(false);
        } else {
            btnactualizar.setVisible(false);
            btnguardar.setVisible(true);
        }
    }

    private void hablitarBotones() {
        if ((cboCurso.getSelectedIndex() != 0) && (cboCursoEspecial.getSelectedIndex() != 0)) {
            btnpasar.setEnabled(true);
            btnpasartodo.setEnabled(true);
            btnregresar.setEnabled(true);
            btnregresartodo.setEnabled(true);
            btnguardar.setEnabled(true);
            btnactualizar.setEnabled(true);

        } else {
            btnpasar.setEnabled(false);
            btnpasartodo.setEnabled(false);
            btnregresar.setEnabled(false);
            btnregresartodo.setEnabled(false);
            btnguardar.setEnabled(false);
            btnactualizar.setEnabled(false);
        }
    }

    private boolean guardarDatos() {
        int total = modeloespecial.size();
        int t = modelonormal.size();
        int s = 0;

        for (int j = 0; j < t; j++) {
            lCurso.setSelectedIndex(j);
            if (objMatricula.ExistenciaMatricula(lCurso.getSelectedValue().toString(),
                    cboCursoEspecial.getSelectedItem().toString()) == true) {
                objMatricula.eliminarMatricula(cboCursoEspecial.getSelectedItem().toString(), lCurso.getSelectedValue().toString());
            }
        }

        for (int i = 0; i < total; i++) {
            lCursoEspecial.setSelectedIndex(i);
            if (objMatricula.ExistenciaMatricula(lCursoEspecial.getSelectedValue().toString(), cboCursoEspecial.getSelectedItem().toString()) == false) {
                objMatricula.curso.sacarIdCursoAsignado(cboCursoEspecial.getSelectedItem().toString());
                objMatricula.sacarIdMatricula(lCursoEspecial.getSelectedValue().toString());
                if (objMatricula.guardarMatriculaCurso() == false) {
                    s++;
                }
            }
        }

        if (s == 0) {
            frmConsultaMatriculaEspeciales.generartabla();
            return true;

        } else {
            return false;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar4 = new javax.swing.JToolBar();
        btnnuevo = new javax.swing.JButton();
        btnguardar = new javax.swing.JButton();
        btnactualizar = new javax.swing.JButton();
        btnsalir = new javax.swing.JButton();
        panelCurso = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lCurso = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        lCursoEspecial = new javax.swing.JList();
        btnpasartodo = new javax.swing.JButton();
        btnpasar = new javax.swing.JButton();
        btnregresar = new javax.swing.JButton();
        btnregresartodo = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cboCurso = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        cboCursoEspecial = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jToolBar4.setBorder(null);
        jToolBar4.setFloatable(false);
        jToolBar4.setBorderPainted(false);

        btnnuevo.setFocusable(false);
        btnnuevo.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnnuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnnuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnnuevoActionPerformed(evt);
            }
        });
        jToolBar4.add(btnnuevo);

        btnguardar.setFocusable(false);
        btnguardar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnguardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnguardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnguardarActionPerformed(evt);
            }
        });
        jToolBar4.add(btnguardar);

        btnactualizar.setFocusable(false);
        btnactualizar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnactualizar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnactualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnactualizarActionPerformed(evt);
            }
        });
        jToolBar4.add(btnactualizar);

        btnsalir.setFocusable(false);
        btnsalir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnsalir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnsalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsalirActionPerformed(evt);
            }
        });
        jToolBar4.add(btnsalir);

        panelCurso.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jScrollPane1.setViewportView(lCurso);

        jScrollPane2.setViewportView(lCursoEspecial);

        btnpasartodo.setText(">>");
        btnpasartodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnpasartodoActionPerformed(evt);
            }
        });

        btnpasar.setText(">");
        btnpasar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnpasarActionPerformed(evt);
            }
        });

        btnregresar.setText("<");
        btnregresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnregresarActionPerformed(evt);
            }
        });

        btnregresartodo.setText("<<");
        btnregresartodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnregresartodoActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("CURSO");

        cboCurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboCursoActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("CURSO ESPECIAL");

        cboCursoEspecial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboCursoEspecialActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelCursoLayout = new javax.swing.GroupLayout(panelCurso);
        panelCurso.setLayout(panelCursoLayout);
        panelCursoLayout.setHorizontalGroup(
            panelCursoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCursoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelCursoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(panelCursoLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cboCurso, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelCursoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnregresar, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnregresartodo)
                    .addComponent(btnpasar, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnpasartodo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelCursoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelCursoLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cboCursoEspecial, 0, 311, Short.MAX_VALUE))
                    .addComponent(jScrollPane2))
                .addContainerGap())
        );
        panelCursoLayout.setVerticalGroup(
            panelCursoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCursoLayout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(panelCursoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cboCursoEspecial, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(panelCursoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(cboCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2)))
                .addGroup(panelCursoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelCursoLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(panelCursoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE)
                            .addComponent(jScrollPane2))
                        .addContainerGap())
                    .addGroup(panelCursoLayout.createSequentialGroup()
                        .addGap(167, 167, 167)
                        .addComponent(btnpasartodo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnpasar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnregresar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnregresartodo)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBar4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelCurso, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar4, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(panelCurso, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        panelCurso.getAccessibleContext().setAccessibleName("Asignar Especiales");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnnuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnnuevoActionPerformed
        iniciar();

    }//GEN-LAST:event_btnnuevoActionPerformed

    private void btnguardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnguardarActionPerformed
        if (guardarDatos() == true) {
            global.Mensajes.mostrarMensaje("Correcto!", "Datos ingresados correctamente en el Curso " + cboCursoEspecial.getSelectedItem() + "..!", 1);
            iniciar();

        } else {
            global.Dialogos.Error("Error al matricular los alumnos");
        }
    }//GEN-LAST:event_btnguardarActionPerformed

    private void btnsalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsalirActionPerformed
        dispose();
        iniciar();

    }//GEN-LAST:event_btnsalirActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        iniciar();
    }//GEN-LAST:event_formWindowClosed

    private void btnpasartodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnpasartodoActionPerformed
        int j = modelonormal.size();
        System.out.println(j);
        if (modelonormal.isEmpty() != true) {
            for (int i = 0; i < j; i++) {
                modeloespecial.addElement(lCurso.getSelectedValue());
                lCursoEspecial.setModel(modeloespecial);
                modelonormal.removeElementAt(lCurso.getSelectedIndex());
                lCurso.setSelectedIndex(0);
                lCursoEspecial.setSelectedIndex(0);
            }
        } else {
            Dialogos.Error("No hay estudiantes para asignar");
        }
    }//GEN-LAST:event_btnpasartodoActionPerformed

    private void btnpasarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnpasarActionPerformed
        if (modelonormal.isEmpty() != true) {
            modeloespecial.addElement(lCurso.getSelectedValue());
            lCursoEspecial.setModel(modeloespecial);
            modelonormal.removeElementAt(lCurso.getSelectedIndex());
            lCurso.setSelectedIndex(0);
            lCursoEspecial.setSelectedIndex(0);
        } else {
            Dialogos.Error("No hay estudiantes para asignar");
        }
    }//GEN-LAST:event_btnpasarActionPerformed

    private void btnregresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnregresarActionPerformed
        if (modeloespecial.isEmpty() != true) {
            modelonormal.addElement(lCursoEspecial.getSelectedValue());
            lCurso.setModel(modelonormal);
            modeloespecial.removeElementAt(lCursoEspecial.getSelectedIndex());
            lCurso.setSelectedIndex(0);
            lCursoEspecial.setSelectedIndex(0);
        } else {
            Dialogos.Error("No hay estudiantes asignado");
        }
    }//GEN-LAST:event_btnregresarActionPerformed

    private void btnregresartodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnregresartodoActionPerformed
        int j = modeloespecial.size();
        if (modeloespecial.isEmpty() != true) {
            for (int i = 0; i < j; i++) {
                modelonormal.addElement(lCursoEspecial.getSelectedValue());
                lCurso.setModel(modelonormal);
                modeloespecial.removeElementAt(lCursoEspecial.getSelectedIndex());
                lCurso.setSelectedIndex(0);
                lCursoEspecial.setSelectedIndex(0);
            }
        } else {
            Dialogos.Error("No hay estudiantes asignado");
        }
    }//GEN-LAST:event_btnregresartodoActionPerformed

    private void cboCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboCursoActionPerformed
        if (cboCurso.getSelectedIndex() != 0) {
            modelonormal = new DefaultListModel();
            lCurso.setModel(modelonormal);
            objMatricula.listaAlumnoCurso(lCurso, modelonormal, cboCurso.getSelectedItem().toString());
            hablitarBotones();

        } else {
            hablitarBotones();
        }
        /*if ((cboCurso.getSelectedIndex() != 0)) {
         modelonormal = new DefaultListModel();
         lCurso.setModel(modelonormal);
         objMatricula.listaAlumnoCurso(lCurso, modelonormal, cboCurso.getSelectedItem().toString(),
         cboCursoEspecial.getSelectedItem().toString());
         hablitarBotones();
         } else {
         hablitarBotones();
         }*/

    }//GEN-LAST:event_cboCursoActionPerformed

    private void cboCursoEspecialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboCursoEspecialActionPerformed
        if ((cboCursoEspecial.getSelectedIndex() != 0)) {
            hablitarBotones();
            modeloespecial = new DefaultListModel();
            lCursoEspecial.setModel(modeloespecial);
            objMatricula.listaAlumnoCursoEspeciales(lCursoEspecial, modeloespecial,
                    cboCursoEspecial.getSelectedItem().toString());

        } else {
            hablitarBotones();
        }
    }//GEN-LAST:event_cboCursoEspecialActionPerformed

    private void btnactualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnactualizarActionPerformed
        if (guardarDatos() == true) {
            global.Mensajes.mostrarMensaje("Correcto!", "Datos actualizados correctamente en el Curso " + cboCursoEspecial.getSelectedItem() + "..!", 1);
            iniciar();
            dispose();
        } else {
            global.Dialogos.Error("Error al actualizar los datos");
        }
    }//GEN-LAST:event_btnactualizarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(dlgAsignacionAlumnoCursoEspeciales.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                dlgAsignacionAlumnoCursoEspeciales dialog = new dlgAsignacionAlumnoCursoEspeciales(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnactualizar;
    private javax.swing.JButton btnguardar;
    private javax.swing.JButton btnnuevo;
    private javax.swing.JButton btnpasar;
    private javax.swing.JButton btnpasartodo;
    private javax.swing.JButton btnregresar;
    private javax.swing.JButton btnregresartodo;
    private javax.swing.JButton btnsalir;
    public javax.swing.JComboBox cboCurso;
    public javax.swing.JComboBox cboCursoEspecial;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar jToolBar4;
    private javax.swing.JList lCurso;
    private javax.swing.JList lCursoEspecial;
    private javax.swing.JPanel panelCurso;
    // End of variables declaration//GEN-END:variables
}
